<template>
  <div v-loading="loading" class="app-container">
    <el-button size="mini" @click="getList()">加载列表</el-button>
    <el-button size="mini" @click="$router.push({path:'/robot/add'})">新建机器人</el-button>
    <el-button type="primary" size="mini" @click="batchRun()">批量启动</el-button>
    <el-button type="primary" size="mini" @click="batchStop()">批量停止</el-button>
    <el-table
      :data="robotList"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width=""
      />
      <el-table-column
        prop="ext1"
        label="编号"
        width=""
      />
      <el-table-column
        prop="name"
        label="名称"
        width=""
      />
      <el-table-column
        prop="pair"
        label="交易对"
        width=""
      />
      <el-table-column
        prop="strategyId"
        label="策略"
        width=""
      />
      <el-table-column
        prop="exchangeId"
        label="账户"
        width=""
      />
      <el-table-column
        prop=""
        label="操作"
      >
        <template slot-scope="scope">
          <el-switch
            v-if="false"
            v-model="scope.row.status"
            active-text="启动"
            inactive-text="停止"
            active-value="1"
            inactive-value="0"
            @change="rest(scope.row)"
          />
          <el-button-group>
            <el-button type="primary" size="mini" :disabled="scope.row.status === 1" @click="rest(scope.row)">启动</el-button>
            <el-button type="warning" size="mini" :disabled="scope.row.status === 0" @click="stop(scope.row)">
              停止
            </el-button>

            <el-button type="primary" size="mini" plain @click="$router.push({path:'/robot/log' ,query:{robotId:scope.row.id,robotName:scope.row.name}})">详情</el-button>
            <el-button type="primary" size="mini" plain @click="$router.push({path:'/robot/setRobot' ,query:{robotId:scope.row.id,robotName:scope.row.name}})">参数设置</el-button>
            <el-button type="danger" size="mini" @click="remove(scope.row.id)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import sapi from '@/api/sysApi'
export default {
  data() {
    return {
      loading: false,
      robotList: [],
      selectedList: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleSelectionChange(val) {
      this.selectedList = val
    },
    batchRun() {
      const func = sapi.RestartRobot
      const funcName = '启动'
      const status = 1
      this.batchHandle(func, funcName, status)
    },
    batchStop() {
      const func = sapi.StopRobot
      const funcName = '停止'
      const status = 0
      this.batchHandle(func, funcName, status)
    },
    batchHandle(func, funcName, status) {
      if (this.selectedList.length < 1) { return this.$message(`请先选择！`) }
      const loading = this.$loading({
        lock: true
      })
      const prmArr = this.selectedList.map(p => func(p.id))
      Promise.allSettled(prmArr).then(ps => {
        let msg = ''
        ps.forEach((p, index) => {
          if (p.value && p.value.data) { this.selectedList[index].status = status } else { msg += `${this.selectedList[index].name},` }
        })
        if (msg) { this.$message(`部分[${funcName}]执行失败：${msg}`) } else { this.$message(`批量[${funcName}]执行完毕:${prmArr.length}个`) }
      }).finally(() => {
        loading.close()
      })
    },
    getList() {
      this.loading = true
      sapi.getRobotList().then((resp) => {
        const data = resp.data
        this.$message(`列表加载完成`)
        if (data) {
          this.robotList = data
        }
      })
        .finally(p => {
          this.loading = false
        })
    },
    rest(item) {
      if (item.status === 1) {
        stop(item)
      } else {
        sapi.RestartRobot(item.id).then((p) => {
          this.$message(`${item.name}启动完成`)
          if (p) item.status = 1
        })
      }
    },
    stop(item) {
      sapi.StopRobot(item.id).then((p) => {
        this.$message(`${item.name}停止完成`)
        if (p) item.status = 0
      })
    },
    onSubmit() {
      this.$message('submit!')
      sapi.TrendSet()
    },
    remove(id) {
      this.$confirm(`此操作将永久删除此机器人:${id},如运行中请先停止机器人, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sapi.RemoveRobot(id).then(p => {
          if (p) { this.$message('删除完成') }
        })
      })
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>

